<?php

namespace app\api\controller\v3;

use app\common\service\pay\alipay\AliPayWap;
use app\common\service\pay\PayService;
use app\common\service\pay\wechat\H5;
use app\common\service\pay\wechat\JsApi;
use app\model\CouponOrder;
use app\model\PayLog;
use think\Exception;
use think\facade\Log;
use think\facade\View;
use think\response\Redirect;

class Pay
{
    protected $middleware = [
        \app\middleware\ApiChannel::class => ['except' => ['webPay','transit']],
        \app\middleware\ApiUserAuthV2::class => ['except' => ['webPay','transit']],
    ];

    public function union()
    {

        $methond = (new PayLog())->payMethond();


        $params = request()->all();
        $msg = dmjValidate([
            'order_id' => 'require|number',
            'pay_method' => 'require|in:' . implode(',', array_keys($methond)),
        ], [], $params);
        if ($msg) {
            return fail($msg);
        }

        $couponOrder = CouponOrder::query()->where('order_id', $params['order_id'])->find();

        if (!$couponOrder) {
            return fail('订单不存在');
        }
        if ($couponOrder->pay_status == CouponOrder::C_PAY_STATUS_YES) {
            return fail('订单已支付');
        }

        $pay_method_class = $methond[$params['pay_method']];

        /**
         * @var H5
         * @var JsApi
         * @var AliPayWap
         */
        try {

            $data = (new PayService($couponOrder))->payByCouponOrder(new $pay_method_class());

            return success('', $data);

        } catch (Exception $exception) {
            Log::info($exception->getFile() . $exception->getLine());
            return fail($exception->getMessage());

        }


    }


    /**
     * 中转转
     */
    public function transit()
    {

        $url = input('url');

        if (empty($url)) {
            return '缺少url错误';
        }

        return redirect($url);


    }

    /**
     * 中间页支付
     */
    public function webPay()
    {

        $methond = (new PayLog())->payMethond();
        $params = request()->all();
        $msg = dmjValidate([
            'order_id' => 'require|number',
            'redirect_url' => 'require',
            'pay_method' => 'require|in:' . implode(',', array_keys($methond)),
        ], [], $params);
        if ($msg) {
            return $msg;
        }
        /**
         * @var CouponOrder $couponOrder
         */
        $couponOrder = CouponOrder::query()->where('order_id', $params['order_id'])->find();

        if (!$couponOrder) {
            return '订单不存在';
        }
        if ($couponOrder->pay_status == CouponOrder::C_PAY_STATUS_YES) {
            return '订单已支付';
        }

        $pay_method_class = $methond[$params['pay_method']];

        /**
         * @var H5
         * @var JsApi
         * @var AliPayWap
         */
        try {

            $obj = new $pay_method_class();

            $data = (new PayService($couponOrder))->payByCouponOrder($obj);

            if (!empty($data['mweb_url'])){
                $mweb_url = $data['mweb_url'];
                echo '正在跳转支付';
                echo "<script language='javascript' type='text/javascript'>window.location.href='$mweb_url'</script>";
            }
//            return View::fetch('api@pay/wechatH5', $data);

        } catch (Exception $exception) {

            return $exception->getMessage();

        }


        //支付宝支付

        //微信支付

        //微信jsapi

        //微信h5支付


    }
}